﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2011
// admin@franknagl.de
//
namespace SBIP.Filter
{
    /// <summary>
    /// Squaring channel filter.
    /// </summary>
    /// 
    /// <remarks>
    /// <para>
    /// The filter squares every color channel. The <see cref="Threshold"/> 
    /// property determines, if the square result will be used alone or 
    /// will be subtract from white color value. The <see cref="InvertSquareChannel"/>
    /// property determines, if the normal or the inverted squarinf filter will 
    /// be used.
    /// </para>
    /// 
    /// <para>Sample usage:</para>
    /// <code>
    /// Bitmap image = new Bitmap( "cathedral.jpg" );
    ///
    /// // 1. Offline rendering of images
    /// // create Processor, used as rendering framework
    /// Processor processor = new Processor( );
    /// // starts Processor
    /// processor.Begin( image );
    /// // create SquareChannel filter
    /// SquareChannel filter = new SquareChannel( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    /// ...
    /// // apply the filter, render to bitmap  
    /// Bitmap resultImage = processor.RenderToBitmap( );
    /// // apply the filter, render to texture
    /// // Texture2D resultTexture = processor.RenderToTexture( );
    /// processor.End( );
    ///  
    /// // 2. Online rendering of videos or images
    /// // create any windows control for rendering in
    /// Form myForm = new Form
    /// {
    ///     Width = 1280,
    ///     Height = 720
    /// };
    /// // starts Processor
    /// processor.Begin( "Video.avi", myForm );
    /// // create SquareChannel filter
    /// filter = new SquareChannel( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    /// ...
    /// // apply the filter
    /// myForm.Show( );
    /// while ( myForm.Created )
    /// {
    ///     processor.Render( );
    ///     Application.DoEvents( );
    /// }
    /// processor.End( );
    /// </code>
    /// 
    /// <para><b>Initial image:</b></para>
    /// <img src="../../Cathedral.jpg" width="500" height="338" />
    /// <para><b>Result image (Threshold = 127):</b></para>
    /// <img src="../../SquareChannel.jpg" />
    /// <para><b>Result image (Inverted square filter):</b></para>
    /// <img src="../../SquareChannelInverted.jpg" />
    /// </remarks>
    public class SquareChannel : BaseFilter
    {
        private bool invertSquareChannel;
        private byte threshold;
        /// <summary>
        /// The threshold for determining, if a channel will be increased or 
        /// decreased.
        /// </summary>
        /// <remarks>Default value: 127.</remarks>
        public byte Threshold
        {
            get { return threshold; }
            set
            {
                if (Effect == null)
                    throw new System.ArgumentException("Threshold" + InitMsg);

                threshold = value;
                Effect.Parameters["Threshold"].SetValue(Threshold);
            }
        }

        /// <summary>
        /// Determines, if the square channel filter works inverted or normal.
        /// </summary>
        /// <remarks>Default value: false.</remarks>
        public bool InvertSquareChannel
        {
            get { return invertSquareChannel; }
            set
            {
                if (Effect == null)
                    throw new System.ArgumentException("InvertSquareChannel" + InitMsg);

                invertSquareChannel = value;
                Technique = value ? "SquareChannelInvert" : "SquareChannel";
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SquareChannel"/> class.
        /// </summary>
        public SquareChannel() : base("SquareChannel") { }

        /// <summary>
        /// Sets the start value of property <see cref="Threshold"/>.
        /// </summary>
        protected override void PostInit()
        {
            Threshold = 127;
        }

    }
}
